package com.blog.backend.utils;

import com.blog.backend.config.CodeMsg;

/**
 * 结果集返回封装
 * @param <T>
 */
public class Result<T> {
    private String message;
    private int code;
    private T data;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private Result(T data, String message, int retCode) {
        this.code = retCode;
        this.message = message;
        this.data = data;
    }

    public Result() {
    }

    private Result(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.code = cm.getCode();
        this.message = cm.getMsg();
    }
    /**
     * 成功时候的调用
     * @return
     */
    public static <T> Result<T> success(T data){

        return new Result<T>(data,"success",200);
    }
    /**
     * 成功，不需要传入参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> Result<T> success(){
        return (Result<T>) success("");
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static <T> Result<T> error(CodeMsg cm){
        return new Result<T>(cm);
    }

    public static <T> Result<T> failure(T data,String message,Integer code){
        return new Result<T>(data,message,code);
    }

}